<?php
   class Checkrate extends MY_Controller{
	   public function __construct(){
		   parent::__construct();
		   $this->load->helper(array("url","form"));
		   $this->load->library("form_validation");
		   $this->load->model("model_rate");
		   $this->load->library('cart');
	   }
	   public function index(){
		   $data['act'] = 1;
		   $this->cart->destroy();	
		   $data['title'] = "Check ticket for Hanoi Sapa Train";
		   $data['trains'] = $this->listtrains(30);
		   $data['listtrains'] = $this->listtrains(4);
		   $now = strtotime(date("m/d/Y"));
		   $tomorow = date('m/d/Y',mktime(0,0,0,date('m',$now),date('d',$now) + 1,date('Y',$now)));
		   $nexttomor = date('m/d/Y',mktime(0,0,0,date('m',$now),date('d',$now) + 2,date('Y',$now)));
		   $curtrain = $data['trains'][0];
		   $curtype = 2;
		   $adults = 1;
		   $data['typetick'] = 2;
		   $curstart = $this->findday($tomorow);
		   $curend = $this->findday($nexttomor);
		   $data['liststart'] = $this->model_rate->getstart($curtrain['train_id'],$curtype,$curstart);
		   $data['listend'] = $this->model_rate->getend($curtrain['train_id'],$curtype,$curend);
		   //$this->debug($data['listend']);
		   $data['curtrain'] = $this->model_rate->gettrain($curtrain['train_id']);
		   $data['trainid'] = $curtrain['train_id'];
		   $data['curstart'] = $tomorow;
		   $data['curendate'] = $nexttomor;
		   $data['adults'] = $adults;
		   if(isset($_POST['okie'])){
			   if(isset($_POST['type'])){
				   $type = $_POST['type'];
			   }else{
				   $type = "";
			   }
			   $train = $_POST['train'];
			   if($train == 0){die("Please select a train");}
			   $start = $_POST['depart'];
			   $adults = $_POST['adult'];
			   $stardate = $this->findday($start);
			   if($type == 2){
			   if(strtotime($start) > strtotime($_POST['return'])){
				   die("Return Date must be greater than depart");
			   }}
			   if($type == 2){
				   $endate = $_POST['return'];
				   $enddate = $this->findday($endate);
				   $data['listend'] = $this->model_rate->getend($train,$type,$enddate);
			   }elseif($type == 1){
				   $endate = "";
				   $enddate = "";
				   $data['listend'] = array();
			   }else{ die("Please select type of ticket");}
			   $data['curtrain'] = $this->model_rate->gettrain($train);
			   $data['trainid'] = $train;
			   $data['curstart'] = $start;
			   $data['curendate'] = $endate;
			   $data['typetick'] = $type;
			   $data['adults'] = $adults;
			   $data['liststart'] = $this->model_rate->getstart($train,$type,$stardate);
			   //echo $train." - ".$type." - ".$stardate; die();
			   //$this->debug($data['liststart']);
			   $this->load->view("checkrate/layout",$data);
		   }else{
		   	   $this->load->view("checkrate/layout",$data);
		   }
	   }
	   public function ajax(){
		    if(!isset($_POST)){die();}
			$this->cart->destroy();	
		   	$type = $_POST['type'];
			$train = $_POST['trainid'];
			$adults = $_POST['adults'];
			if($train == 0){die("Please select a train");}
			$start = $_POST['depart'];
			$stardate = $this->findday($start);
			$endda = $_POST['returns'];
			$curtrain = $this->model_rate->gettrain($train);
			if($type == 2){
			if(strtotime($start) > strtotime($endda)){
				   die("Return Date must be greater than depart");
			}}
		    if($type == 2){
			   $endate = $_POST['returns'];
			   $enddate = $this->findday($endate);
			   $listend = $this->model_rate->getstart($train,$type,$enddate);
		    }elseif($type == 1){
			   $endate = "";
			   $enddate = "";
			   $listend = array();
		    }else{ die("Please select type of ticket");}
			$liststart = $this->model_rate->getstart($train,$type,$stardate);
		  	?>
            <div id="result">
                <div class="clearfix box-schedule" id="divAjax">
                  <input type="hidden" name="train_id" value="18"/>
                  <input type="hidden" id="tickty" value="<?php echo $type; ?>"/>
                  <p class="line25"> <b>Hanoi - Sapa on <?php echo $curtrain['train_name']; ?></b> </p>
                  <p class="line12"> Departing:
                    <input type="hidden" id="trainname" value="<?php echo $curtrain['train_name']; ?>" />
                    <input type="text" value="<?php echo $start; ?>" id="tickstart" class="input-text hadate" readonly="readonly">
                  </p>
                  <p class="numPeople line">
					<label id="lbAdult">Number of passengers : <?php echo $adults; ?></label>
				  </p>
                  <table class="tableStyle">
                    <tr>
                      <th>Departs</th>
                      <th>Arrives</th>
                      <th>Duration</th>
                      <th>Train</th>
                      <th>Soft Seat</th>
                      <th>Hard Berth</th>
                      <th>Soft berth</th>
                      <th>Vip Berth</th>
                    </tr>
                    <?php if(isset($liststart) && $liststart != NULL){ ?>
                    <?php foreach($liststart as $tickstart){ ?>
                    <tr>
                      <td><?php echo $tickstart['tickdeparts']; ?></td>
                      <td><?php echo $tickstart['tickarrives']; ?></td>
                      <td><?php echo $tickstart['tickduration']; ?></td>
                      <td><a title="" target="_blank">SP3</a></td>
                      <td><?php if($tickstart['softseat'] != 0){ echo "<span><input class='radprice' datatype='1' datafrom='Hanoi' datato='Sapa' dataname='Soft Seat' dataid='".$tickstart['tickid']."' value='".$tickstart['softseat']."' name='rooms".$tickstart['tickid']."' type='radio' />&nbsp;$".$tickstart['softseat']."</span>"; }else{ echo "<span>N/A</span>";} ?></td>
                      <td><?php if($tickstart['hardberth'] != 0){ echo "<span><input class='radprice' datatype='1' datafrom='Hanoi' datato='Sapa' dataname='Hard Berth' dataid='".$tickstart['tickid']."' value='".$tickstart['hardberth']."' name='rooms".$tickstart['tickid']."' type='radio' />&nbsp;$".$tickstart['hardberth']."</span>"; }else{ echo "<span>N/A</span>";} ?></td>
                      <td><?php if($tickstart['softberth'] != 0){ echo "<span><input class='radprice' datatype='1' datafrom='Hanoi' datato='Sapa' dataname='Soft berth' dataid='".$tickstart['tickid']."' value='".$tickstart['softberth']."' name='rooms".$tickstart['tickid']."' type='radio' />&nbsp;$".$tickstart['softberth']."</span>"; }else{ echo "<span>N/A</span>";} ?></td>
                      <td><?php if($tickstart['vipberth'] != 0){ echo "<span><input class='radprice' datatype='1' datafrom='Hanoi' datato='Sapa' dataname='Vip Berth' dataid='".$tickstart['tickid']."' value='".$tickstart['vipberth']."' name='rooms".$tickstart['tickid']."' type='radio' />&nbsp;$".$tickstart['vipberth']."</span>"; }else{ echo "<span>N/A</span>";} ?></td>
                    </tr>
                    <?php } }else{ ?>
                    <tr>
                      <th colspan="8" style="background:none;color:#000">No tickets available</th>
                    </tr>
                    <?php } ?>
                  </table>
                  <p class="line25"> <b>Sapa - Hanoi on <?php echo $curtrain['train_name']; ?></b> </p>
                  <p class="line12"> Return:
                    <input type="text" value="<?php echo $endate; ?>" id="tickend" class="input-text hadate" readonly="readonly" />
                  </p>
                  <p class="numPeople line">
					<label id="lbAdult">Number of passengers : <?php echo $adults; ?></label>
				  </p>
                  <table class="tableStyle">
                    <tr>
                      <th>Departs</th>
                      <th>Arrives</th>
                      <th>Duration</th>
                      <th>Train</th>
                      <th>Soft Seat</th>
                      <th>Hard Berth</th>
                      <th>Soft berth</th>
                      <th>Vip Berth</th>
                    </tr>
                    <?php if(isset($listend) && $listend != NULL){ ?>
                    <?php foreach($listend as $tickend){ ?>
                    <tr>
                      <td><?php echo $tickend['tickdeparts']; ?></td>
                      <td><?php echo $tickend['tickarrives']; ?></td>
                      <td><?php echo $tickend['tickduration']; ?></td>
                      <td><a title="" target="_blank">SP3</a></td>
                      <td><?php if($tickend['softseat'] != 0){ echo "<span><input class='radprice' datatype='2' datafrom='Sapa' datato='Hanoi' dataname='Soft Seat' dataid='".$tickend['tickid']."' value='".$tickend['softseat']."' name='rooms".$tickend['tickid']."' type='radio' />&nbsp;$".$tickend['softseat']."</span>"; }else{ echo "<span>N/A</span>";} ?></td>
                      <td><?php if($tickend['hardberth'] != 0){ echo "<span><input class='radprice' datatype='2' datafrom='Sapa' datato='Hanoi' dataname='Hard Berth' dataid='".$tickend['tickid']."' value='".$tickend['hardberth']."' name='rooms".$tickend['tickid']."' type='radio' />&nbsp;$".$tickend['hardberth']."</span>"; }else{ echo "<span>N/A</span>";} ?></td>
                      <td><?php if($tickend['softberth'] != 0){ echo "<span><input class='radprice' datatype='2' datafrom='Sapa' datato='Hanoi' dataname='Soft berth' dataid='".$tickend['tickid']."' value='".$tickend['softberth']."' name='rooms".$tickend['tickid']."' type='radio' />&nbsp;$".$tickend['softberth']."</span>"; }else{ echo "<span>N/A</span>";} ?></td>
                      <td><?php if($tickend['vipberth'] != 0){ echo "<span><input class='radprice' datatype='2' datafrom='Sapa' datato='Hanoi' dataname='Vip Berth' dataid='".$tickend['tickid']."' value='".$tickend['vipberth']."' name='rooms".$tickend['tickid']."' type='radio' />&nbsp;$".$tickend['vipberth']."</span>"; }else{ echo "<span>N/A</span>";} ?></td>
                    </tr>
                    <?php } }else{ ?>
                    <tr>
                      <th colspan="8" style="background:none;color:#000">No tickets available</th>
                    </tr>
                    <?php } ?>
                  </table>
                  <div id="ticket-total-bound">
                    <div class="left-ttb" style="float:left">
                      <div class="line num-pass">
                        <h3 class="line6">Number of Passengers</h3>
                        <select class="Fl" id="adults" name="adults">
                          <?php for($i=1;$i<=10;$i++){ ?>
                          <option value="<?php echo $i; ?>" <?php if($adults == $i){ echo "selected='selected'";} ?>><?php echo $i; ?></option>
                          <?php } ?>
                        </select>
                      </div>
                    </div>
                    <div class="right-ttb" style="float:right;">
                      <p align="right" class="lineH16 line"> <b class="size13 line3">Ticket Total:
                        <label id="lblPrice">$ 0</label>
                        </b><br>
                        <span class="c_a0">* This price is exclusive of credit card processing fee (4%)</span> </p>
                    </div>
                  </div>
                  <p class="line12">
                    <a href="<?php echo base_url()."extra-services.html?train=".$curtrain['train_id']; ?>" class="btn btn-red continue none" id="btnContinue"><span>Continue</span></a>
                  </p>
                </div>
                <div class="tab-ticket-bound" >
                  <div class="right-tab-content2222">
                    <div class="tab-child overflow">
                      <ul class="list">
                        <li class="active"><a href="#overiew-routes"><span><b>Overview</b></span></a></li>
                        <li class=""><a href="#overiew-fearture"><span><b>Fearture</b></span></a></li>
                        <li class=""><a href="#overiew-eat-sleep"><span><b>Eat &amp; Sleep</b></span></a></li>
                        <li class=""><a href="#overiew-attractions"><span><b>Attractions</b></span></a></li>
                        <li class=""><a href="#overiew-direction"><span><b>Direction</b></span></a></li>
                      </ul>
                    </div>
                    <div class="clear"></div>
                    <div class="tab-small clearfix" id="overiew-routes" style="display: block;">
                      <p> <strong><?php echo $curtrain['train_name']; ?></strong></p>
                      <p><?php echo $curtrain['train_overview']; ?></p>
                    </div>
                    <div style="display: none;" class="tab-small clearfix" id="overiew-fearture"> </div>
                    <div style="display: none;" class="tab-small clearfix" id="overiew-eat-sleep"> </div>
                    <div style="display: none;" class="tab-small clearfix" id="overiew-attractions"> </div>
                    <div style="display: none;" class="tab-small clearfix" id="overiew-direction"> </div>
                  </div>
                </div>
                </div>
            <?php
	   }
	   public function findday($orgDate){
	      $strto = strtotime($orgDate);
	      $retDAY = date('Y-m-d', mktime(0,0,0,date('m',$strto),date('d',$strto),date('Y',$strto)));
	      return $retDAY;
	  }
   }
